package cn.lyjuan.android.demofive.ui.slide.load;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import cn.lyjuan.android.demofive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 下拉刷新
 * Created by chad on 2016/3/27.
 */
public class SwipeRefreshActivity extends Activity
{
    private List<String> list;
    private ArrayAdapter<String> adp;

    private ListView listView;
    private SwipeRefreshLayout swipe;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.ui_slide_load_swipe_refresh);

        initComponent();    // 获取控件引用

        setComponent();     // 设置控件属性

        initData();         // 加载数据

        initBind();         // 加载绑定事件

        Log.w(this.getClass().getSimpleName(), "java.specification.vendor >> " + System.getProperty("java.specification.vendor"));
    }

    /**
     * 加载绑定事件
     */
    private void initBind()
    {
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Toast.makeText(SwipeRefreshActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                        swipe.setRefreshing(false);// 设置不在刷新状态
                    }
                }, 5000);
            }
        });
    }

    private void setComponent()
    {
        swipe.setColorSchemeResources(android.R.color.holo_blue_light);
    }

    /**
     * 加载控件
     */
    private void initComponent()
    {
        swipe = (SwipeRefreshLayout) this.findViewById(R.id.ui_slide_load_swipe_refresh);
        listView = (ListView) this.findViewById(R.id.ui_slide_load_swipe_refresh_list);
    }

    /**
     * 加载数据
     */
    private void initData()
    {
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++)
        {
            list.add(Integer.toString(i));
        }

        adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);

        listView.setAdapter(adp);

        adp.notifyDataSetChanged();
    }
}
